package com.da.landlord;

import java.util.Iterator;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.googlecode.cswish.annotation.cm;
import com.ra.landlord.FeeRuleCfg;
import com.ra.landlord.LandlordOwner;
import com.ra.landlord.LandlordOwnerUser;

/**
 * 费用规则
 * 
 * @author Peter.Z
 *
 */
@Entity
@Table(name="da_dafeerule")
public class DaFeeRule implements LandlordOwner {

	private Integer id;

	private DaSuite suite;

	@cm("对应的是子表")
	private DaRoom room;
	
	private List<DaMeter> meters;
	
	private FeeRuleCfg feeRuleCfg;

	private LandlordOwnerUser landlordUser;

	private List<DaChargeItem> chargeItems;
	
	//transient
	@cm("上次抄表")
	private DaMeter lastMeter;
	
	@cm("上上次抄表")
	private DaMeter lastLastMeter;
	
	// transient, 指定某一计费规则下， 该规则的本次读表数据和上次读表数据
	@cm("本次抄表")
	private DaMeter currentMeter;

	//抄表时注意：如匹配电表下的所有房源时，必须要是state=1的房源才有效: select * from suite where id in(select suite_id from feeRule where state=1 and id=xxx)  
	@cm("表示此费用规则的有效性(null无效) (1有效) ")
	private Integer state;

	@Id
	@GeneratedValue
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@ManyToOne(fetch=FetchType.LAZY)
	public DaRoom getRoom() {
		return room;
	}

	public void setRoom(DaRoom room) {
		this.room = room;
	}
	

	@ManyToOne(fetch=FetchType.LAZY)
	public DaSuite getSuite() {
		return suite;
	}

	public void setSuite(DaSuite suite) {
		this.suite = suite;
	}

	@ManyToOne //unique = true,
	@JoinColumn(name="cfgId",referencedColumnName="FeeItemId")
	public FeeRuleCfg getFeeRuleCfg() {
		return feeRuleCfg;
	}

	public void setFeeRuleCfg(FeeRuleCfg feeRuleCfg) {
		this.feeRuleCfg = feeRuleCfg;
	}

	@ManyToOne
	public LandlordOwnerUser getLandlordUser() {
		return landlordUser;
	}

	public void setLandlordUser(LandlordOwnerUser landlordUser) {
		this.landlordUser = landlordUser;
	}

	@OneToMany(mappedBy = "feeRule")
	@OrderBy("dateEndRecv desc")
	public List<DaChargeItem> getChargeItems() {
		return chargeItems;
	}

	public void setChargeItems(List<DaChargeItem> chargeItems) {
		this.chargeItems = chargeItems;
	}
	
	@Transient
	public DaMeter getLastMeter() {
		return lastMeter;
	}

	public void setLastMeter(DaMeter lastMeter) {
		this.lastMeter = lastMeter;
	}
	
	@Transient
	public DaMeter getLastLastMeter() {
		return lastLastMeter;
	}

	public void setLastLastMeter(DaMeter lastLastMeter) {
		this.lastLastMeter = lastLastMeter;
	}
	
	@Transient
	public DaMeter getCurrentMeter() {
		return currentMeter;
	}

	public void setCurrentMeter(DaMeter currentMeter) {
		this.currentMeter = currentMeter;
	}
	
	public Integer getState() {
		return state;
	}

	public void setState(Integer state) {
		this.state = state;
	}

	@OneToMany(mappedBy = "feeRule",fetch=FetchType.LAZY,cascade = CascadeType.ALL)
	@OrderBy("checkTime desc")
	public List<DaMeter> getMeters() {
		return meters;
	}

	public void setMeters(List<DaMeter> meters) {
		this.meters = meters;
	}
	

	public DaMeter getMeter(int index) {
		Iterator<DaMeter> iter = getMeters().iterator();
		DaMeter meter = null;
		while (index >= 0 && iter.hasNext()) {
			index--;
			meter = iter.next();
		}
		return meter;
	}

}
